package com.yanqu.road.dao.impl.activity.horserun.game;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.horserun.HorseRunUserGamingData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HorseRunUserGamingDataDaoImpl extends TempDataObjectDao<HorseRunUserGamingData> {
    public static String getHorseRunUserGamingDataSql = "select * from `t_u_horse_run_activity_gaming_data` where `activityId`=? and `userId`=?";
    public HorseRunUserGamingData getHorseRunUserGamingData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getHorseRunUserGamingDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateHorseRunUserGamingDataSql = "update `t_u_horse_run_activity_gaming_data` set processId=?,stageId=?,eventId=?,eventState=?,sumExp=?,openGameMultiple=?,backGround=?,eventTypeTriggerTimes=?,gamingBaseAttr=?,gamingTotalAttr=?,skill=?,gamingGrade=?,eventMap=?,cardEventMap=?,remainExploreTime=?,nowConfident=?,maxConfident=?,expEventStage=?,chooseOneEventAttrType=?,battleHorseId=?,unlockNewEquip=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(HorseRunUserGamingData horseRunUserGamingData) {
        horseRunUserGamingData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getProcessId()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getStageId()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getEventId()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getEventState()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getSumExp()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getOpenGameMultiple()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getBackGround()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getEventTypeTriggerTimes())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getGamingBaseAttr())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getGamingTotalAttr())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getSkill())));
        para.put(i++, new DbParameter(Types.BIGINT, horseRunUserGamingData.getGamingGrade()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getCardEventMap())));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getRemainExploreTime()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getNowConfident()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getMaxConfident()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getExpEventStage())));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getChooseOneEventAttrType()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getBattleHorseId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getUnlockNewEquip())));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, horseRunUserGamingData.getUserId()));
        if (getDb().executeNoneQuery(updateHorseRunUserGamingDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHorseRunUserGamingDataSql = "insert into `t_u_horse_run_activity_gaming_data`(activityId,userId,processId,stageId,eventId,eventState,sumExp,openGameMultiple,backGround,eventTypeTriggerTimes,gamingBaseAttr,gamingTotalAttr,skill,gamingGrade,eventMap,cardEventMap,remainExploreTime,nowConfident,maxConfident,expEventStage,chooseOneEventAttrType,battleHorseId,unlockNewEquip) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HorseRunUserGamingData horseRunUserGamingData) {
        horseRunUserGamingData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, horseRunUserGamingData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getProcessId()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getStageId()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getEventId()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getEventState()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getSumExp()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getOpenGameMultiple()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getBackGround()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getEventTypeTriggerTimes())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getGamingBaseAttr())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getGamingTotalAttr())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getSkill())));
        para.put(i++, new DbParameter(Types.BIGINT, horseRunUserGamingData.getGamingGrade()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getEventMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getCardEventMap())));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getRemainExploreTime()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getNowConfident()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getMaxConfident()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getExpEventStage())));

        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getChooseOneEventAttrType()));
        para.put(i++, new DbParameter(Types.INTEGER, horseRunUserGamingData.getBattleHorseId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(horseRunUserGamingData.getUnlockNewEquip())));
        if (getDb().executeNoneQuery(insertHorseRunUserGamingDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HorseRunUserGamingData parseData(ResultSet rs) throws SQLException {
        HorseRunUserGamingData horseRunUserGamingData = new HorseRunUserGamingData();
        horseRunUserGamingData.setActivityId(rs.getInt("activityId"));
        horseRunUserGamingData.setUserId(rs.getLong("userId"));
        horseRunUserGamingData.setProcessId(rs.getInt("processId"));
        horseRunUserGamingData.setStageId(rs.getInt("stageId"));
        horseRunUserGamingData.setEventId(rs.getInt("eventId"));
        horseRunUserGamingData.setEventState(rs.getInt("eventState"));
        horseRunUserGamingData.setSumExp(rs.getInt("sumExp"));
        horseRunUserGamingData.setOpenGameMultiple(rs.getInt("openGameMultiple"));
        horseRunUserGamingData.setBackGround(rs.getInt("backGround"));
        horseRunUserGamingData.setEventTypeTriggerTimes(JSON.parseObject(rs.getString("eventTypeTriggerTimes"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        horseRunUserGamingData.setGamingBaseAttr(JSON.parseObject(rs.getString("gamingBaseAttr"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.lang.Integer>>() {}));
        horseRunUserGamingData.setGamingTotalAttr(JSON.parseObject(rs.getString("gamingTotalAttr"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.lang.Integer>>() {}));
        horseRunUserGamingData.setSkill(JSON.parseObject(rs.getString("skill"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        horseRunUserGamingData.setGamingGrade(rs.getLong("gamingGrade"));
        horseRunUserGamingData.setEventMap(JSON.parseObject(rs.getString("eventMap"), new TypeReference<ConcurrentHashMap<Integer, List<List<Integer>>>>() {}));
        horseRunUserGamingData.setCardEventMap(JSON.parseObject(rs.getString("cardEventMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.lang.Integer>>() {}));
        horseRunUserGamingData.setRemainExploreTime(rs.getInt("remainExploreTime"));
        horseRunUserGamingData.setNowConfident(rs.getInt("nowConfident"));
        horseRunUserGamingData.setMaxConfident(rs.getInt("maxConfident"));
        horseRunUserGamingData.setExpEventStage(JSON.parseObject(rs.getString("expEventStage"), new TypeReference<List<java.lang.Integer>>() {}));

        horseRunUserGamingData.setChooseOneEventAttrType(rs.getInt("chooseOneEventAttrType"));
        horseRunUserGamingData.setBattleHorseId(rs.getInt("battleHorseId"));
        horseRunUserGamingData.setUnlockNewEquip(JSON.parseObject(rs.getString("unlockNewEquip"), new TypeReference<List<Integer>>() {}));

        return horseRunUserGamingData;
    }

}
